<template>
  <div>
    <el-input v-model="id"></el-input>
    <el-button @click="login1">使用redis实现幂等性</el-button>
    <el-button @click="login2">使用数据库去重表实现幂等性</el-button>

    <section class="step-section">
      <span>1、</span>
      <span>pom.xml文件：</span>
      <span>spring-boot-starter-data-redis</span>
      <span>+</span>
      <span>jedis</span>
    </section>
    <section class="step-section">
      <span>2、</span>
      <span>配置类：</span>
      <span>RedisConfig.java</span>
    </section>
    <section class="step-section">
      <span>3、</span>
      <span>核心代码：</span>
      <span>Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, idempotent, 1L, TimeUnit.DAYS);</span>
    </section>
  </div>
</template>

<script>
import API from '@/api/idempotent'
export default {
  name: 'idempotent',
  data () {
    return {
        id: 1
    }
  },
  methods: {
    login1 () {
      API.login1({id: this.id}).then(res => {
        this.$message.success(`操作结果为：` + res.data)
      }).catch(e => {
        this.$message.error(`error:${e.message}`)
      }).finally(() => {

      })
    },
    login2 () {
      API.login2({id: this.id}).then(res => {
        this.$message.success(`操作结果为：` + res.data)
      }).catch(e => {
        this.$message.error(`error:${e.message}`)
      }).finally(() => {

      })
    }
  }
}
</script>

<style lang="less" scoped>
.step-section{
  font-size: 16px;
  font-weight: bold;
}
</style>
